//package com.sx.manage.util;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.InputStream;
//import java.io.UnsupportedEncodingException;
//import java.net.URL;
//import java.net.URLEncoder;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Date;
//
//import org.apache.commons.lang3.StringUtils;
//
//import com.aliyun.oss.ClientException;
//import com.aliyun.oss.HttpMethod;
//import com.aliyun.oss.OSSClient;
//import com.aliyun.oss.OSSException;
//import com.aliyun.oss.model.GeneratePresignedUrlRequest;
//import com.aliyun.oss.model.ObjectMetadata;
//
//public class OSSUtils {
//	
//	private static final String ACCESS_ID = "LTAIN0ULAt5eP4Js";
//    private static final String ACCESS_KEY = "udmX30XBNq3AqCsrHAbHLScUb8voZ7";
//    private static final String BUCKET_NAME = "bucket_name";
//    private static final String PROTOCOL = "http";
////    private static final String OSS_ENDPOINT = "oss-cn-beijing.aliyuncs.com";
//    private static final String OSS_ENDPOINT = "oss_endpoint";
//	private static final String OSS_ENDPOINT_OUT = "oss_endpoint_out";
//   // 内网==OSS_URL=oss-cn-beijing-internal.aliyuncs.com
//    //	外网==	OSS_URL=oss-cn-beijing.aliyuncs.com
//    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");
//    
//    private static String contentType(String filename){
//    	String FilenameExtension = FileUtils.getFileExt(filename);
//		if(FilenameExtension.equals("BMP")||FilenameExtension.equals("bmp")){return "image/bmp";}
//		if(FilenameExtension.equals("GIF")||FilenameExtension.equals("gif")){return "image/gif";}
//		if(FilenameExtension.equals("JPEG")||FilenameExtension.equals("jpeg")||
//		   FilenameExtension.equals("JPG")||FilenameExtension.equals("jpg")||	
//		   FilenameExtension.equals("PNG")||FilenameExtension.equals("png")){return "image/jpeg";}
//		if(FilenameExtension.equals("HTML")||FilenameExtension.equals("html")){return "text/html";}
//		if(FilenameExtension.equals("TXT")||FilenameExtension.equals("txt")){return "text/plain";}
//		if(FilenameExtension.equals("VSD")||FilenameExtension.equals("vsd")){return "application/vnd.visio";}
//		if(FilenameExtension.equals("PPTX")||FilenameExtension.equals("pptx")||
//			FilenameExtension.equals("PPT")||FilenameExtension.equals("ppt")){return "application/vnd.ms-powerpoint";}
//		if(FilenameExtension.equals("DOCX")||FilenameExtension.equals("docx")||
//			FilenameExtension.equals("DOC")||FilenameExtension.equals("doc")){return "application/msword";}
//		if(FilenameExtension.equals("XML")||FilenameExtension.equals("xml")){return "text/xml";}
//		if(FilenameExtension.equals("PDF")||FilenameExtension.equals("pdf")){return "application/pdf";}
//		return "";
//	 }
//    
//    /**
//     * 上传至自定义目录
//     * @param file
//     * @param path
//     * @return
//     * @throws OSSException
//     * @throws ClientException
//     * @throws FileNotFoundException
//     */
//    public static String uploadFile(File file, String path)  throws OSSException, ClientException, FileNotFoundException {
//    	if(StringUtils.isBlank(path)){
//    		path = "";
//    	}
//        if(StringUtils.isNotBlank(path) && !path.endsWith("/")){
//            path = path + "/";
//        }
//        String endpoint = PROTOCOL + "://" + PropertiesUtils.getProperty(OSS_ENDPOINT_OUT) + "/";
//        OSSClient client = new OSSClient(endpoint, ACCESS_ID, ACCESS_KEY);
//        ObjectMetadata objectMeta = new ObjectMetadata();
//        objectMeta.setContentLength(file.length());
//        objectMeta.setContentType(contentType(file.getName()));
//        InputStream input = new FileInputStream(file);
//        String fileName =  file.getName().replaceAll(" ", "");
//        String bucket_name = PropertiesUtils.getProperty(BUCKET_NAME);
//        client.putObject(bucket_name, path + fileName, input, objectMeta);
//        try {
//			fileName = URLEncoder.encode(fileName, "utf-8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
//        client.shutdown();
//        return  path + fileName+"/";
//    }
//    
//    /**
//     * 上传无用户属性的图片值Bucket/images目录
//     * @param file
//     * @return
//     * @throws OSSException
//     * @throws ClientException
//     * @throws FileNotFoundException
//     */
//    public static String uploadImage(File file) throws OSSException, ClientException, FileNotFoundException{
//    	String bucketPath = "images/";
//    	return uploadFile(file, bucketPath);
//    }
//    
//    
//    /**
//     * 上传无用户属性的文件至Bucket根目录
//     * @param file
//     * @return
//     * @throws OSSException
//     * @throws ClientException
//     * @throws FileNotFoundException
//     */
//    public static String uploadFile(File file) throws OSSException, ClientException, FileNotFoundException{
//    	String bucketPath = "";
//    	return uploadFile(file, bucketPath);
//    }
//    
//    /**
//     * 上传图片至当前用户目录
//     * @param file
//     * @param currentUserId
//     * @return
//     * @throws OSSException
//     * @throws ClientException
//     * @throws FileNotFoundException
//     */
//    public static String uploadImage(File file, String currentUserId) throws OSSException, ClientException, FileNotFoundException{
//    	if (StringUtils.isEmpty(currentUserId)){
//    		return uploadImage(file);
//    	}
//    	String bucketPath = currentUserId+"/images";
//    	return uploadFile(file, bucketPath);
//    }
//    
//    
//    /**
//     * 上传简历只当前用户目录
//     * @param file
//     * @param currentUserId
//     * @return
//     * @throws OSSException
//     * @throws ClientException
//     * @throws FileNotFoundException
//     */
//    public static String uploadResume(File file, String currentUserId) throws OSSException, ClientException, FileNotFoundException{
//    	if (StringUtils.isEmpty(currentUserId)){
//    		return uploadFile(file);
//    	}
//    	String bucketPath = "resumes/"+currentUserId;
//    	return uploadFile(file, bucketPath);
//    }
//    
//    public static String uploadResumeByDate(File file) throws OSSException, ClientException, FileNotFoundException{
//    	String date = SDF.format(new Date());
//    	String bucketPath = "resumes/"+date;
//    	return uploadFile(file, bucketPath);
//    }
//    
//    /**
//	 * 获取临时下载链接
//	 * 
//	 * @param file
//	 * @param path
//	 * @return
//	 */
//	public static String getFileUrl(String fileName, String path) {
//		String endpoint = PROTOCOL + "://" + PropertiesUtils.getProperty(OSS_ENDPOINT_OUT) + "/";
//		// 服务器端生成url签名字串
//		OSSClient server = new OSSClient(endpoint, ACCESS_ID, ACCESS_KEY);
//		Calendar nowTime = Calendar.getInstance();
//		nowTime.add(Calendar.MINUTE, 5);
//		GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(PropertiesUtils.getProperty(BUCKET_NAME), path + "/" + fileName, HttpMethod.GET);
//		// 设置过期时间
//		request.setExpiration(nowTime.getTime());
//		// 生成URL签名(HTTP GET请求)
//		URL signedUrl = server.generatePresignedUrl(request);
//		String returnUrl = signedUrl.toString();
//		server.shutdown();
//		return returnUrl;
//	}
//
//}
